package com.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mypg.Comment;
import com.mypg.CommentHelper;
import com.mypg.UserQuestion;
import com.service.UserQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/user_question")
@Controller
public class UserQuestionController {

    @Autowired
    UserQuestionService userQuestionService;

    @RequestMapping("/user_question_table_select.do")
    @ResponseBody
    public String doUser_question_table_select(String user_nickname, String question_id,
                                         int page, int limit) throws ParseException, JsonProcessingException {
        int start_row=(page-1)*limit;
        int page_nums=limit;
        int question_id_int=0;
        if(question_id==null||question_id=="")
            question_id_int=0;
        else{
            question_id_int=Integer.parseInt(question_id);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",userQuestionService.selectUserQuestion_count_For_user_question_table_jsp(user_nickname,question_id_int));
        List<UserQuestion> userQuestionList=userQuestionService.selectUserQuestion_for_user_question_table_jsp(user_nickname,question_id_int,start_row,page_nums);
        map.put("data", userQuestionList);
        ObjectMapper mapper=new ObjectMapper();
        String map_json_str=mapper.writeValueAsString(map);
        return map_json_str;
    }

    @RequestMapping("/user_question_table_delete_one.do")
    @ResponseBody
    public void doUser_question_table_delete_one(int user_id,int question_id){
        userQuestionService.deleteUserQuestionByUserQuestion_id(user_id,question_id);
    }

    @RequestMapping("/user_question_table_delete_many.do")
    @ResponseBody
    public int doUser_question_table_delete_many(String[] user_id_array,String[] question_id_array){
        user_id_array[0]=user_id_array[0].replace("[","");
        user_id_array[user_id_array.length-1]=user_id_array[user_id_array.length-1].replace("]","");
        question_id_array[0]=question_id_array[0].replace("[","");
        question_id_array[question_id_array.length-1]=question_id_array[question_id_array.length-1].replace("]","");
        for(int i=0;i<user_id_array.length;i++){
            userQuestionService.deleteUserQuestionByUserQuestion_id(Integer.parseInt(user_id_array[i]),
                    Integer.parseInt(question_id_array[i]));
        }
        return 0;
    }
}
